<template>
  <div>
    <div class="loading-wrapper" v-show="showLoading">
      <Loading></Loading>
    </div>
    <div class="register-wrapper" v-show="!showLoading">
      <!-- <img :src="imgUrl" alt="" width="100%" height="100%"> -->
      <div id="register">
        <p class="title">用户注册</p>
        <el-form :model="UserLIst" status-icon :rules="rules" ref="UserLIst"  label-width="0"  class="demo-ruleForm" >
          <el-form-item prop="userName">
            <el-input v-model="UserLIst.userName" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="loginName">
            <el-input v-model="UserLIst.loginName"  placeholder="请输入创建账号名"></el-input>
          </el-form-item>
        
          <el-form-item prop="loginPswd">
            <el-input type="password" v-model="UserLIst.loginPswd" autocomplete="off" placeholder="输入密码"></el-input>
          </el-form-item>
          <el-form-item prop="checkPass">
            <el-input type="password" v-model="UserLIst.checkPass"  autocomplete="off" placeholder="确认密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('UserLIst')" style="width:100%;">注册</el-button>
             
            <p class="login" @click="gotoLogin">已有账号？立即登录</p>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

import Loading from '@/components/Loading.vue'
export default {
  name: "Register",
  components: {
    Loading
  },
  data() {

    var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.UserLIst.checkPass !== '') {
            this.$refs.UserLIst.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.UserLIst.loginPswd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return {
      showLoading: true,
      imgUrl: require('../../assets/images/bg-img-1.jpg'),
      UserLIst: {
        userName: "",
        loginName:'',
        loginPswd: "",
        checkPass: "",
        
      },
      rules:{
        loginPswd:[{validator: validatePass, trigger: 'blur'}],
        checkPass:[{ validator: validatePass2, trigger: 'blur'}],
                  
      }



    }
  },
  mounted () {
    let bgImg = new Image()
    bgImg.src = this.imgUrl
    bgImg.onerror = () => {
      console.log('img onerror')
    }
    bgImg.onload = () => { // 图片加载成功后 去除 loading
      this.showLoading = false
    }
  },
  methods: {
    getUsers(){
        
    },
    gotoLogin() {
      this.$router.push({
        path: "/login"
      });
    },
     submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
          this.$axios.get("user/register",{params:this.UserLIst}).then(res=>{
           this.$router.push({
             name:'login'
           })
          }
        )
          } else {
            alert('请确认密码一致')
            return false;
          }
        });
      },
     
    }
  
  
};
</script>

<style scoped>
.loading-wrapper {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background: #aedff8;
  display: flex;
  align-items: center;
  justify-content: center;
}
.register-wrapper img {
  position: absolute;
   display: block;
  z-index: 1;
}
.register-wrapper {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
}
#register {
  max-width: 340px;
  margin: 60px auto;
  background: #fff;
  padding: 20px 40px;
  border-radius: 10px;
  position: relative;
  z-index: 9;
}
.title {
  font-size: 26px;
  line-height: 50px;
  font-weight: bold;
  margin: 10px;
  text-align: center;
}
.el-form-item {
  text-align: center;
}
.login {
  margin-top: 10px;
  font-size: 14px;
  line-height: 22px;
  color: #1ab2ff;
  cursor: pointer;
  text-align: left;
  text-indent: 8px;
  width: 160px;
}
.login:hover {
  color: #2c2fd6;
}
.code >>> .el-form-item__content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.code button {
  margin-left: 20px;
  width: 140px;
  text-align: center;
}
.el-button--primary:focus {
  background: #409EFF;
  border-color: #409EFF;
  color: #fff;
}
@media (max-width: 768px) {
  #register {
    max-width: 260px;
    margin: 60px auto;
    background: #fff;
    padding: 10px 20px;
    border-radius: 10px;
    position: relative;
    z-index: 9;
  }
}
</style>
